<?php
/**
 * SiteController
 * 
 * @package cha
 * @author blog.anchen8.net
 * @copyright 2014
 * @version $Id$
 * @access public
 */
class SiteController extends AdminController 
{
    protected function beforeAction($action){
		parent::beforeAction($action);
		
		return true;
	}
    /**
     * SiteController::actionIndex()
     * 
     * @return void
     */
    public function actionIndex() {
        $CDbCriteria = new CDbCriteria();
	    $CDbCriteria->order = '`siteid` desc';
	    $iCount = AdminSite::model()->count($CDbCriteria);
	    $oPages = new CPagination($iCount);
	     
	    $oPages->pageSize=$this->pagesize;
	    $oPages->applyLimit($CDbCriteria);
	    $oModel = AdminSite::model()->findAll($CDbCriteria);
	    
	    $this->render('index',array('model'=>$oModel,'pages'=>$oPages));
	}
    /**
     * SiteController::actionCreate()
     * 
     * @return void
     */
    public function actionCreate(){
	    $oModel = new AdminSite();
	    if(isset($_POST['ajax']) && $_POST['ajax']==='site-_form-form')
	    {
	        echo CActiveForm::validate($oModel);
	        Yii::app()->end();
	    }
	    if(isset($_POST['AdminSite'])){
	        $oModel->attributes=$_POST['AdminSite'];
            $oModel->setting = json_encode($_POST['AdminSite']['setting']) ;
	        if($oModel->save()){
	            $alert="SendWebMsg('success','添加站点成功','".$this->createUrl('site/index')."')";
                //修改对配置文件
                $aParamsConfig = include(Yii::app()->basePath .'/config/params.php');
                //$aMainCongfig = include(Yii::app()->basePath .'/config/main.php');
                $aParamsConfig['setting'] = $_POST['AdminSite']['setting'];
                $aParamsConfig['setting']['watermark'] = $_POST['AdminSite']['watermark'];
                $aParamsConfig['name'] =  $_POST['AdminSite']['name'];
                @file_put_contents(Yii::app()->basePath .'/config/params.php' ,"<?php\n return ".var_export($aParamsConfig ,true)."\n?>");
                //file_put_contents(Yii::app()->basePath .'/config/params.php' ,'adfads');
	        }else{
	            if($oModel->hasErrors()){
	                $alert="SendWebMsg('cue','请修正页面错误提示项')";
	            }else{
	                $alert="SendWebMsg('error','你的网络不给力，操作失败')";
	            }
	        }
	        Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
	    }
        
	    $this->render('_form',array('model'=>$oModel));
	}
    /**
     * SiteController::actionUpdate()
     * 
     * @param mixed $sid
     * @return void
     */
    public function actionUpdate($sid){;
	    $model = AdminSite::model()->findByPk($sid);
	    if(isset($_POST['ajax']) && $_POST['ajax']==='site-_form-form')
	    {
	        echo CActiveForm::validate($model);
	        Yii::app()->end();
	    }
	    if(isset($_POST['AdminSite'])){
	        $model->attributes = $_POST['AdminSite'];
            $model->setting = json_encode($_POST['AdminSite']['setting']) ;
	        if($model->save()){
	            //修改对配置文件
                $aParamsConfig = include(Yii::app()->basePath .'/config/params.php');
                $aMainCongfig = include(Yii::app()->basePath .'/config/main.php');
                $aParamsConfig['setting'] = $_POST['AdminSite']['setting'];
                $aParamsConfig['setting']['watermark'] = $_POST['AdminSite']['watermark'];
                $aParamsConfig['name'] =  $_POST['AdminSite']['name'];
                @file_put_contents(Yii::app()->basePath .'/config/params.php' ,"<?php\n return ".var_export($aParamsConfig ,true)."\n?>");                 
                
	            $alert="SendWebMsg('success','站点设置更新成功','".$this->createUrl('site/index')."')";
	        }else{
	            if($model->hasErrors()){
	                $alert="SendWebMsg('cue','请修正页面错误提示项')";
	            }else{
	                $alert="SendWebMsg('error','你的网络不给力，操作失败')";
	            }
	        }
            
	        Yii::app()->getClientScript()->registerScript('msg',$alert,CClientScript::POS_END);
	    }
	    
	    $this->render('_form',array('model'=>$model));
	}
    /**
     * SiteController::actionDelete()
     * 
     * @param mixed $sid
     * @return void
     */
    public function actionDelete($sid){
	    $result = AdminSite::model()->findByPk($sid);        
	    if($result && $result->delete()){
			$this->ajaxReturn(0,'删除成功');
		}else{
			$this->ajaxReturn(1,'删除失败');
		}
	}
}